前一天已經安裝好Python了,那麼這一天來介紹最基本的資料型態吧!
a_var = 12
_var = "apple"
str = "apple"
1_var = "30"
1-var = "mary"
number = 5
print(type(number))
print(number)
#output
<class 'int'>
5
Float = 6.66
print(type(Float))
print(Float)
#output
<class 'float'>
6.66
string = "123"
print(type(string))
print(string)
#output
<class 'str'>
123
boolean = True
print(type(boolean))
print(boolean)
#output
<class 'bool'>
True
arr = ['what', 'ever', 'you', 'want']
print(type(arr))
for i in arr:
print(i)
#output
<class 'list'>
what
ever
you
want
ex_dict = {'match_key': 'match_value'}
print(type(ex_dict))
print(ex_dict['match_key'])
#output
<class 'dict'>
match_value
str_type = "100"
print(type(str_type))
print(str_type)
int_type = (str_type)
print(type(int_type))
print(int_type)
#output
<class 'str'>
100
<class 'int'>
100
str_type = "100"
print(type(str_type))
print(str_type)
int_type = (str_type)
print(type(int_type))
print(int_type)
#output
<class 'str'>
100
<class 'int'>
100
str_type = "o100o"
print(type(str_type))
print(str_type)
int_type = int(str_type)
print(type(int_type))
print(int_type)
#output
<class 'str'>
o100o
Traceback (most recent call last):
File "d:\ex_data.py", line 121, in <module>
int_type = int(str_type)
^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'o100o'
第1行為正常的字串所以輸出正常,但第5行因為字串內有非0~10的字元存在,導致程式報錯,必須注意被轉換的字串須皆為整數。